// pages/city/index.js
var bmap = require('../../utils/bmap-wx.min.js')
import Common from '../../utils/common.js'
let http = new Common()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    citys: [],
    city2: [],
    city: '',
    Value: '',
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  userLocationShow: function () {
    let that = this
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userLocation']) {
          wx.getLocation({
            success: function (res) {
              that.getLocationData(res)
            }
          })
        } else {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              wx.getLocation({
                success: function (res) {
                  that.getLocationData(res)
                }
              })
            }
          })
        }
      }
    })
  },
  getLocationData(res) {
    let that = this
    let BMap = new bmap.BMapWX({
      ak: 'gra5f4eUdz12CutGCGhgbm7sQ1wpPaGK'
    });

    let latitude = res.latitude //经度   
    let longitude = res.longitude //纬度   
    let speed = res.speed
    let accuracy = res.accuracy
    // console.log('aa')
    BMap.regeocoding({
      location: latitude + ',' + longitude,
      success: function (res) {
       
        that.setData({
          loactionString: res.originalData.result.addressComponent.city
        })
      },
      fail: function (res) {
        // console.log(res)
      }
    })
  },
  inputValue: function (e) {
    this.setData({
      Value: e.detail.value
    })
    // console.log(e.detail.value)
  },
  //点击搜索框
  searchClick: function () {
    let that = this
    http.http({
      url: "mp2/store/searchcity",
      data: {
        cityname: that.data.Value
      },
      success(res) {
        that.setData({
          city2: res.data.data
        })
        // console.log(res)
      }
    })
  },
  getCity: function () {
    let that = this
    http.http({
      url: 'mp2/store/getcity',
      success(res) {
        that.setData({
          citys: res.data.data
        })
        // console.log(res.data.data)
      }
    })
  },
  loactionClick: function (res) {
    let that = this
    let index = res.currentTarget.dataset.index
    // console.log(that.data.citys)
    wx.setStorageSync('city', that.data.loactionString)
    wx.navigateBack({
      delta: 1
    })
  },
  city2Click: function (res) {
    let that = this
    let index = res.currentTarget.dataset.index
    // console.log(that.data.citys)
    wx.setStorageSync('city', that.data.citys[index].name)
    wx.navigateBack({
      delta: 1
    })
  },
  cityClick: function (res) {
    let that = this
    let index = res.currentTarget.dataset.index

    wx.setStorageSync('city', that.data.citys[index].name)

    wx.navigateBack({
      delta: 1
    })
  },
  onLoad: function (options) {
    this.userLocationShow()
    this.getCity()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})